export default {
	data() {
		return {
			h: uni.getSystemInfoSync().windowHeight,
			lineOnelist: [],
			selectShowOne: false,
			selectShowTwo: false,
			hasLocal: false, //是否包含本地人
			isCs: 1, //是否是车船联票 1不是 2是
			isSsGq: false, //出发地或者目的地是否为嵊山或枸杞
			params: {
				from: '',
				to: '',
				startPortNo: '',
				endPortNo: '',
				startDate: new Date().Format('yyyy-MM-dd'),
				pzlx: 0
			},

			leftShow: false,
			isLeftSsGq: false,
			params1: {
				city: '',
				from: '',
				to: '',
				startPortNo: '',
				endPortNo: '',
				startDate: new Date().Format('yyyy-MM-dd'),
				pzlx: 0,
				carShip: false //是否是车船联票
			},
			leftItem: null,
			leftLineList: [],

			rightShow: false,
			isRightSsGq: false,
			params2: {
				from: '',
				to: '',
				startPortNo: '',
				endPortNo: '',
				startDate: new Date().Format('yyyy-MM-dd'),
				pzlx: 0,
				carShip: false //是否是车船联票
			},
			rightItem: null,
			rightLineList: [],
			
			titleOne:'',
			titleTwo:'填写订单'
		}
	},
	onShow() {
		uni.removeStorageSync('shipType');
		this.getIsLocal();
	},
	methods: {
		closePopup(type) {
			if (type == 1) this.selectShowOne = false;
			else this.selectShowTwo = false;
			
			this.isCs = 1;
			this.isSsGq = false;
			this.isLeftSsGq = false;
			this.isRightSsGq = false;
			
			this.step = 0;
			this.cbItem = null;
			this.ppItem = null;
			this.step2 = 0;
			this.adcGoItem = null;
			this.adcBackItem = null;
			this.ppGoItem = null;
			this.ppBackItem = null;
		},
		showCxghOne() {
			// if (this.params.from.indexOf('上海') > -1) { //上海出发的则是车船联票
			// 	this.isCs = 2;
			// 	this.params.pzlx = 1;
			// }
			this.titleOne = this.params.from+'-'+this.params.to;
			this.getLineListOne(this.params);
			this.selectShowOne = true;
		},
		getIsLocal() {
			if (!uni.getStorageSync('userInfo')) return
			this.$api.get('/api/commonPerson/isLocalCount', {
				openId: JSON.parse(uni.getStorageSync('userInfo')).openId,
				isLocal: 1
			}).then(res => {
				if (res.data.code === 0) {
					this.hasLocal = res.data.data > 0 ? true : false;
				} else this.$showModal(res.data.msg);
			})
		},
		getLineListOne(params) {
			this.$api.get('/travel/isshipport/pageLineAll', {
				startPortNo: params.startPortNo,
				endPortNo: params.endPortNo,
				startDate: params.startDate,
				pzlx: params.pzlx
			}).then(res => {
				if (res.data.code === 0) {
					this.lineOnelist = res.data.data || [];
					this.isEmpty(params.from, params.to, this.lineOnelist);
				} else this.$showModal(res.data.msg)
			})
		},
		//空数据时处理
		isEmpty(from, to, list) {
			if (list.length == 0) {
				if ((from.indexOf('嵊山') > -1 || from.indexOf('枸杞') > -1) || (to.indexOf('嵊山') > -1 || to.indexOf('枸杞') >
						-1)) this.isSsGq = true;
				else this.isSsGq = false;
			}
		},
		showCxghTwo() {
			this.getLineListLeft();
			this.getLineListRight();
			this.selectShowTwo = true;
		},
		getLineListLeft() {
			// this.params1.city = '合肥';
			// this.params1.from = '嵊泗(沈家湾)';
			// this.params1.to = '嵊泗(泗礁)';
			// this.params1.startPortNo = '1028';
			// this.params1.endPortNo = '1010';
			this.params1.yr = new Date(this.params1.startDate).Format('MM-dd');
			this.params1.week = this.getWeek(this.params1.startDate);
			// if (this.params1.from.indexOf('上海') > -1) { //上海出发的则是车船联票
			// 	this.params1.carShip = true;
			// 	this.params1.pzlx = 1;
			// }

			this.$api.get('/travel/isshipport/pageLineAll', {
				startPortNo: this.params1.startPortNo,
				endPortNo: this.params1.endPortNo,
				startDate: this.params1.startDate,
				pzlx: this.params1.pzlx
			}).then(res => {
				if (res.data.code === 0) {
					this.leftLineList = res.data.data || [];
					this.leftLineList.forEach((l, i) => {
						this.$set(this.leftLineList[i], 'isSelect', false);
					})
					if (this.leftLineList.length == 0) {
						if ((this.params1.from.indexOf('嵊山') > -1 || this.params1.from.indexOf('枸杞') > -1) || (
								this.params1.to.indexOf('嵊山') > -1 || this.params1.to.indexOf('枸杞') > -1)) this
							.isLeftSsGq = true;
						else this.isLeftSsGq = false;
					}
				} else this.$showModal(res.data.msg)
			})
		},
		selectLeftItem(item, index) {
			this.leftLineList.forEach((l, i) => {
				this.$set(this.leftLineList[i], 'isSelect', i == index ? true : false);
			});
			this.leftItem = item;
			this.judgmentTime();
		},
		getLineListRight() {
			// this.params2.from = '嵊泗(沈家湾)';
			// this.params2.to = '嵊泗(泗礁)';
			// this.params2.startPortNo = '1028';
			// this.params2.endPortNo = '1010';
			this.params2.yr = new Date(this.params2.startDate).Format('MM-dd');
			this.params2.week = this.getWeek(this.params2.startDate);
			// if (this.params2.from.indexOf('上海') > -1) { //上海出发的则是车船联票
			// 	this.params2.carShip = true;
			// 	this.params2.pzlx = 1;
			// }

			this.$api.get('/travel/isshipport/pageLineAll', {
				startPortNo: this.params2.startPortNo,
				endPortNo: this.params2.endPortNo,
				startDate: this.params2.startDate,
				pzlx: this.params2.pzlx
			}).then(res => {
				if (res.data.code === 0) {
					this.rightLineList = res.data.data || [];
					this.rightLineList.forEach((l, i) => {
						this.$set(this.rightLineList[i], 'isSelect', false);
					})
					if (this.rightLineList.length == 0) {
						if ((this.params2.from.indexOf('嵊山') > -1 || this.params2.from.indexOf('枸杞') > -1) || (
								this.params2.to.indexOf('嵊山') > -1 || this.params2.to.indexOf('枸杞') > -1)) this
							.isRightSsGq = true;
						else this.isRightSsGq = false;
					}
				} else this.$showModal(res.data.msg)
			})
		},
		selectRightItem(item, index) {
			this.rightLineList.forEach((l, i) => {
				this.$set(this.rightLineList[i], 'isSelect', i == index ? true : false);
			});
			this.rightItem = item;
			this.judgmentTime();
		},
		judgmentTime() {
			if (this.leftItem && this.rightItem) {

			}
		},
		leftConfirm(e) {
			this.params1.startDate = e[0];
			this.params1.yr = new Date(e[0]).Format('MM-dd');
			this.params1.week = this.getWeek(e[0]);
			this.getLineListLeft();
			this.leftShow = false;
		},
		rightConfirm(e) {
			this.params2.startDate = e[0];
			this.params2.yr = new Date(e[0]).Format('MM-dd');
			this.params2.week = this.getWeek(e[0]);
			this.getLineListRight();
			this.rightShow = false;
		},
		getWeek(date) {
			let week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
			let n = new Date().Format('yyyy-MM-dd');
			let d = new Date(date).getDay();
			let mt = new Date(new Date().setDate(new Date().getDate() + 1)).Format('yyyy-MM-dd');
			return date == n ? '今天' : (date == mt ? '明天' : week[d]);
		},
		selectFlight(item, t) {
			if (item.lineState != 0) return
			let type = '';
			if (this.isCs == 2) { //车船联票
				type = 'ship';
				uni.setStorageSync('shipType', this.isCs);
			}
			
			this.step = 1; 
			this.cbItem = item;
			this.$nextTick(()=>{
				this.$refs.cxghConfirmBooking.init(item);
			})
		},
		buyTicket() {
			if (!this.leftItem) return this.$showToast('请选择第一程船票');
			if (!this.rightItem) return this.$showToast('请选择第二程船票');
			
			if(this.leftItem.timeMemosMin){
				let oneEndTime = '';
				if(this.leftItem.busStartTime){
					let newTime = new Date(this.leftItem.sailDateTime).Format('yyyy-MM-dd')+' '+this.leftItem.busStartTime+':00';
					oneEndTime = new Date(new Date(newTime).setMinutes(new Date(newTime).getMinutes() + this.leftItem.timeMemosMin)).Format('yyyy-MM-dd hh:mm:ss');
				}else{
					oneEndTime = new Date(new Date(this.leftItem.sailDateTime).setMinutes(new Date(this.leftItem.sailDateTime).getMinutes() + this.leftItem.timeMemosMin)).Format('yyyy-MM-dd hh:mm:ss');
				}
				
				let diff = Date.parse(this.rightItem.sailDateTime)-Date.parse(oneEndTime);
				if(diff<0){
					return uni.showModal({
						title:'温馨提示',
						content:'第一程到达时间预计晚于第二程出发时间，请选择其他航班或其他出发时间',
						showCancel:false,
						confirmColor:'#007A69'
					})
				}
			}

			let item = {
				goInfo:this.leftItem,
				backInfo:this.rightItem
			};
			this.step2 = 1;
			this.adcGoItem = this.leftItem;
			this.adcBackItem = this.rightItem;
			this.$nextTick(()=>{
				this.$refs.cxghArriveDepartConfirm.init(item);
			})
		}
	}
}